package com.cslg.questionnaires.mapper;

import com.cslg.questionnaires.entity.Question;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

@Mapper
public interface QuestionMapper {
    @Select("SELECT * FROM question WHERE owerid = #{owerid} AND owertype = #{owertype} AND del = 0 ORDER BY id DESC") //LIMIT #{startline},10
    List<Question> getQuestionByOwerid(int owerid,String owertype,int startline);

    @Insert("INSERT INTO question(owerid,owertype,title,content,starttime,finishtime,isexamine) " +
            "VALUES (#{owerid},#{owertype},#{title},#{content},#{starttime},#{finishtime},#{isexamine})")
    int addQuestion(Question question);

    @Select("SELECT isexamine FROM question WHERE id = #{questionId} AND owerid = #{userId} AND owertype = #{owertype}")
    Integer getExamineState(int questionId,int userId,String owertype);

    @Select("SELECT * FROM question WHERE owertype = 'Student' AND isexamine = 0 AND del =0")
    List<Question> getUnAuditQuestion();

    @Update("UPDATE question SET isexamine = 1 WHERE id = #{questionId}")
    Integer passAuditByQuestionId(int questionId);

    @Update("UPDATE question SET isexamine = 9 WHERE id = #{questionId}")
    Integer rejectAuditByQuestionId(int questionId);

    @Select("SELECT * FROM question WHERE id = #{questionId} AND owerid = #{userId} AND owertype = #{userType}")
    Map<String,Object> getQuestionById_uType_uId(int questionId,String userType,int userId);

    @Select("SELECT * FROM question WHERE id = #{questionId}")
    Question getQuestionById(int questionId);

    @Select("SELECT * FROM question WHERE id = #{id} AND owerid = #{owerid} AND owertype = #{owerType}")
    Question getQuestionById_uId_oType(int id,int owerid,String owerType);

    @Update("UPDATE question SET title = #{title},content = #{content}," +
            "starttime = #{starttime},finishtime = #{finishtime},isexamine = #{isexamine} " +
            "WHERE id = #{id}")
    Integer editQuestion(Question question);

    @Update("UPDATE question SET del = 1 WHERE id = #{questionid} AND owerid = #{userid} AND owertype = #{owertype}")
    int delQuestionById(int questionid,int userid,String owertype);
}
